# encoding: utf-8
""" 
@author:Daniel 
@license: Apache Licence 
@file: token_utils.py 
@time: 2020/07/17
@contact: tongdangdang1@yy.com
"""

from utils.sctoken import *

class TokenUtils(object):
    VALID_TIME = 100 * 24 * 3600

    @classmethod
    def gen_token(cls, app_id, app_secret, uid, valid_time=100 * 24 * 3600):
        app_secret = bytearray(app_secret)
        build_timestamp = int(time.time() * 1000)
        parameter = {"pkey1": "pval1", "pkey2": "pval2"}
        privileges = {"pri1": 1, "pri2": 2}
        token_str = SCToken().gen(app_id, app_secret, uid, parameter, privileges, build_timestamp, valid_time)
        print("Gen: ", token_str, "\n")
        return token_str

    @classmethod
    def parse_token(cls, token_str, app_secret):
        yt, err = SCToken().parse(token_str, app_secret)
        if err != None:
            print("Parse: Fail. ", err)
        else:
            print("Parse: Succ.")
            if yt.validate():
                print("Validate: Succ.")
            else:
                print("Validate: Fail.")

            print(str(yt), "\n")


if __name__ == '__main__':
    token_str = TokenUtils.gen_token(2003426465, b'YQilIPtKss9qWCHk', "123456")

    TokenUtils.parse_token("AAAAAgAAAHB3adyhAAYxMjM0NTYAAgAFcGtleTEABXB2YWwxAAVwa2V5MgAFcHZhbDIAAgAEcHJpMQAAAAAAAAABAARwcmkyAAAAAAAAAAIAAAFzW70WSQAA6mA4Yq9ThqG83ItwbQt_wtIcMBbxtQ", b'YQilIPtKss9qWCHk')
